<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/base.css" media="all">
    <style>
        .pop_panel {
            padding-top: 23px;
            padding-right: 36px;
        }
    </style>
</head>
<body>
<form class="layui-form pop_panel" action="/taskadd" method="POST">
    <div class="layui-form-item">
        <label class="layui-form-label">下载地址</label>
        <div class="layui-input-block">
            <input type="text" name="url" value="" autocomplete="off" placeholder="请输入URL" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">文件名</label>
        <div class="layui-input-block">
            <input type="text" name="name" value="" autocomplete="off" placeholder="请输入下载文件名" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
        </div>
    </div>
</form>
<script src="/static/layui/layui.js" charset="utf-8"></script>
<script>
    //JavaScript代码区域
    layui.use(['element', 'layer', "form"], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            form = layui.form;
        //
        // $('.layui-btn').on('click', function(){
        //     alert("demo");
        // });
        var inputUrl = $("input[name='url']")
        var inputName = $("input[name='name']")

        function getPageName(url)
        {
            var tmp= new Array();//临时变量，保存分割字符串
            tmp=url.split("/");//按照"/"分割
            var pp = tmp[tmp.length-1];//获取最后一部分，即文件名和参数
            tmp=pp.split("?");//把参数和文件名分割开
            return tmp[0];
        };

        $(inputName).focus(function () {
            inputName.val(getPageName(inputUrl.val()))
        });

        form.on('submit(*)', function (data) {
            $.ajax({
                url: data.form.action,
                type: data.form.method,
                data: $(data.form).serialize(),
                success: function (info) {
                    if (info.code == 0) {
                        layer.msg(info.msg);
                        setTimeout(function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                            layer.closeAll();
                            // parent.location.reload();
                        }, 600);
                    } else {
                        layer.msg(info.msg, {
                            icon: 5
                        });
                    }

                }
            });

            return false;
        });

    });
</script>
</body>
</html>